package com.skyline.common.util;

import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

public class MailUtils {
	private static final Log LOGGER = LogFactory.getLog(MailUtils.class);
	private static final JavaMailSender SENDER = SpringUtils.getBean("mailSender");
	private static final String FROM_ADDRESS = "skyline_test@163.com";

	private static SimpleMailMessage createSimpleMessage(String toAddr, String title, String content) {
		return createSimpleMessage(new String[] { toAddr }, null, null, null, title, content);
	}

	private static SimpleMailMessage createSimpleMessage(String[] toAddrs, String[] ccAddrs,
			String[] bccAddrs, String replyToAddr, String title, String content) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom(FROM_ADDRESS);
		if (toAddrs != null) {
			msg.setTo(toAddrs);
		}
		if (ccAddrs != null) {
			msg.setCc(ccAddrs);
		}
		if (bccAddrs != null) {
			msg.setBcc(bccAddrs);
		}
		if (replyToAddr != null) {
			msg.setReplyTo(replyToAddr);
		}
		if (!StringUtils.hasLength(title)) {
			title = "无主题";
		}
		if (!StringUtils.hasLength(content)) {
			content = "信息已丢失。";
		}
		msg.setSentDate(new Date());
		msg.setSubject(title);
		msg.setText(content);
		return msg;
	}

	private static MimeMessage createMimeMessage(String toAddr, String title, String content)
			throws MessagingException {
		return createMimeMessage(new String[] { toAddr }, null, null, null, title, content);
	}

	private static MimeMessage createMimeMessage(String[] toAddrs, String[] ccAddrs,
			String[] bccAddrs, String replyToAddr, String title, String content)
			throws MessagingException {
		MimeMessage msg = SENDER.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true);
		helper.setFrom(FROM_ADDRESS);
		if (toAddrs != null) {
			helper.setTo(toAddrs);
		}
		if (ccAddrs != null) {
			helper.setCc(ccAddrs);
		}
		if (bccAddrs != null) {
			helper.setBcc(bccAddrs);
		}
		if (replyToAddr != null) {
			helper.setReplyTo(replyToAddr);
		}
		if (!StringUtils.hasLength(title)) {
			title = "无主题";
		}
		if (!StringUtils.hasLength(content)) {
			content = "信息已丢失。";
		}
		msg.setSentDate(new Date());
		helper.setSubject(title);
		helper.setText(content, true);
		return msg;
	}

	public static void sendSimpleMail(String toAddress, String title, String content) {
		SimpleMailMessage msg = createSimpleMessage(toAddress, title, content);
		SENDER.send(msg);
	}

	public static void sendSimpleMail(String[] toAddresses, String title, String content) {
		SimpleMailMessage msg = createSimpleMessage(toAddresses, null, null, null, title, content);
		SENDER.send(msg);
	}

	public static void sendHtmlMail(String toAddress, String title, String content) {
		try {
			MimeMessage msg = createMimeMessage(toAddress, title, content);
			SENDER.send(msg);
		} catch (MessagingException e) {
			LOGGER.warn("发送HTML邮件发生错误！", e);
		}

	}

	public static void sendHtmlMail(String[] toAddresses, String title, String content) {
		try {
			MimeMessage msg = createMimeMessage(toAddresses, null, null, null, title, content);
			SENDER.send(msg);
		} catch (MessagingException e) {
			LOGGER.warn("发送HTML邮件发生错误！", e);
		}
	}

	public static void main(String... args) throws MessagingException {
//		sendSimpleMail("wuqihui__1@163.com", "测试邮件", "这是一封测试邮件");
		String title = FreeMarkerUtils.getRegistMailTitle("Liang Chen");
		String content = FreeMarkerUtils.getRegistMailContent("Liang chen", "http://www.renren.com");
		System.out.println(title);
		System.out.println(content);
		sendHtmlMail("panbiping89@163.com", title, content);

	}
}
